// pages/discover/issue/issue.js
const config = require('../../../config')
const util = require('../../../utils/util.js')
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    title: '',
    location:'',
    tagId:'',
    price: '',
    description:'',
    imgUrl: '',
    
    showImg:'',
    tag_index: '',
    address: ['文理学部', '工学部', '信息学部', '医学部'],
    tags: ['二手货', '跑腿'],
    date: '2018-04-01',
    time: '12:01',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 标题
   */
  titleChange: function (e) {
    this.setData({
      title: e.detail.value
    })
  },

  /**
   * 地点
   */
  addressPickerChange: function (e) {
    if (e.detail.value == 0) {
      this.setData({
        location: '文理学部'
      })
    }
    else if (e.detail.value == 1) {
      this.setData({
        location: '工学部'
      })
    }
    else if (e.detail.value == 2) {
      this.setData({
        location: '信息学部'
      })
    } else {
      this.setData({
        location: '医学部'
      })
    }
  },

  /**
   * 标签
   */
  tagPickerChange: function (e) {
    this.setData({
      tag_index: e.detail.value
    })
    if (e.detail.value == 0) {
      this.setData({
				tagId: '2c92a1b8636446c20163645b4bba0001',
      })
    }
    else if (e.detail.value == 1) {
      this.setData({
				tagId: '2c92a1b8636446c20163645b318e0000',
      })
    }
  },

  /**
   * 价格
   */
  priceChange: function (e) {
    this.setData({
      price: e.detail.value
    })
  },

  /**
   * 截止日期
   */
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },

  /**
   * 截止时间
   */
  bindTimeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },

  /**
   * 简介
   */
  descriptionChange: function (e) {
    this.setData({
      description: e.detail.value
    })
  },

  /**
   * 选择图片 
   */
  chooseImage() {
    var that = this
    wx.chooseImage({
      success: function (res) {
        that.setData({
          showImg: res.tempFilePaths
        })
      }
    })
  },

  submit:function(){
    var that = this
    wx.uploadFile({
      url: config.uploadImg,
      filePath: that.data.showImg[0],
      name: 'file',
      formData: {
        'userId': app.globalData.userId
      },
      success: function (res) {
        that.setData({
          imgUrl: res.data
        })
        console.log(res.data)
        var deadline = that.data.date + ' ' + that.data.time
        console.log(deadline)
        util.addInformation(that.data.title, that.data.location, that.data.tagId, that.data.price, that.data.imgUrl,deadline, that.data.description)
        wx.navigateBack({
          delta: 1,
        })
      }, fail: function (err) {
        console.log(err)
      }
    })
  },

  cancel:function(){
    wx.navigateBack({
      delta: 1,
    })
  },

  /**
  * 提交
  */
  formSubmit: function (e) {
    var that = this;
    var formData = e.detail.value;     //获取表单所有input的值    
    wx.request({
      url: '',
      data: formData,
      header: { 'Content-Type': 'application/json' },
      success: function (res) {
        console.log(res.data)
      }
    })
    this.upload()
  },
})